package com.wangyadong.hobby.schedule.jobtask.ath;

import java.util.stream.Stream;

public class MyAtoi {

    public int myatoi(String num) {
        if (num == null || num.equals("")) {
            return 0;
        }
        char[] chars = num.toCharArray();
        char symbol = chars[0];
        boolean symbolFalg = false;
        if (symbol == '-') {
            symbolFalg = true;
        }
        long flag = 1;
        long result = 0;
        for (int i = chars.length - 1; i > 0; i--) {
            char aChar = chars[i];
            boolean b = aChar >= 48 && aChar <= 57;
            if (!b) {
                continue;
            }
            long temp = (long) aChar - 48;
            result += temp * flag;
            flag = flag * 10;
        }
        if (!symbolFalg) {
            char aChar = chars[0];
            boolean b = aChar >= 48 && aChar <= 57;
            if(b){
                long temp = aChar - 48;
                result += temp * flag;
                if (result >= Integer.MAX_VALUE) {
                    return Integer.MAX_VALUE;
                }
                return (int) result;
            }
            return (int) result;
        } else {
            if (result <= Integer.MIN_VALUE) {
                return Integer.MIN_VALUE;
            }
            return (int) result * -1;
        }
    }

    public static void main(String[] args) {
        String s = "+92287hello3333";
        MyAtoi myAtoi = new MyAtoi();
        System.out.println(myAtoi.myatoi(s));
    }
}
